﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Xml.Linq;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace FileBrowserApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadDirectoryTree();
        }

        private void LoadDirectoryTree()
        {
            TreeNode rootNode = new TreeNode("用户目录");
            treeView1.Nodes.Add(rootNode);
            LoadSubDirectories(rootNode, Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
        }

        private void LoadSubDirectories(TreeNode node, string path)
        {
            try
            {
                string[] subDirectories = Directory.GetDirectories(path);
                foreach (string subDirectory in subDirectories)
                {
                    TreeNode subNode = new TreeNode(Path.GetFileName(subDirectory));
                    subNode.Tag = subDirectory;
                    node.Nodes.Add(subNode);
                    LoadSubDirectories(subNode, subDirectory);
                }
            }
            catch (UnauthorizedAccessException) { }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string selectedPath = e.Node.Tag as string;
            if (selectedPath != null)
            {
                LoadFilesAndDirectories(selectedPath);
            }
        }

        private void LoadFilesAndDirectories(string path)
        {
            listView1.Items.Clear();
            listView1.Columns.Clear();
            listView1.Columns.Add("名称", 250);
            listView1.Columns.Add("类型", 100);
            listView1.Columns.Add("大小", 100);

            try
            {
                string[] directories = Directory.GetDirectories(path);
                foreach (string directory in directories)
                {
                    ListViewItem item = new ListViewItem(Path.GetFileName(directory));
                    item.SubItems.Add("文件夹");
                    item.SubItems.Add("");
                    item.Tag = directory;
                    listView1.Items.Add(item);
                }

                string[] files = Directory.GetFiles(path);
                foreach (string file in files)
                {
                    FileInfo fileInfo = new FileInfo(file);
                    ListViewItem item = new ListViewItem(fileInfo.Name);
                    item.SubItems.Add("文件");
                    item.SubItems.Add(fileInfo.Length.ToString());
                    item.Tag = file;
                    listView1.Items.Add(item);
                }
            }
            catch (UnauthorizedAccessException) { }
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                string selectedPath = listView1.SelectedItems[0].Tag as string;
                if (selectedPath != null && Path.GetExtension(selectedPath).ToLower() == ".xml")
                {
                    DisplayXmlContent(selectedPath);
                }
            }
        }

        private void DisplayXmlContent(string filePath)
        {
            try
            {
                XDocument xDocument = XDocument.Load(filePath);
                MessageBox.Show(xDocument.ToString(), "XML 内容");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"无法读取 XML 文件: {ex.Message}");
            }
        }
    }
}